package cn.supermaster.management.web.rest.task;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.web.rest.task
 * @ClassName: ScheduleTaskRestController
 * @Description: [调度任务信息操作处理]
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/2/16 17:04
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/


import cn.supermaster.management.common.annotation.Log;
import cn.supermaster.management.common.enums.BusinessType;
import cn.supermaster.management.common.page.TableDataInfo;
import cn.supermaster.management.common.response.WebResponse;
import cn.supermaster.management.schedule.domain.ScheduleTask;
import cn.supermaster.management.web.service.task.ISTaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/monitor/job")
public class ScheduleTaskRestController {

    @Autowired
    private ISTaskService taskService;

    @RequiresPermissions("monitor:job:list")
    @PostMapping("/list")
    public TableDataInfo queryScheduleTaskByCondition(ScheduleTask task) {
        return taskService.queryScheduleTaskByCondition(task);
    }

    @Log(title = "定时任务", businessType = BusinessType.EXPORT)
    @RequiresPermissions("monitor:job:export")
    @PostMapping("/export")
    public WebResponse doExportScheduleTask(ScheduleTask task) {
        return taskService.doExportScheduleTask(task);
    }


    @Log(title = "定时任务", businessType = BusinessType.DELETE)
    @RequiresPermissions("monitor:job:remove")
    @PostMapping("/remove")
    public WebResponse removeScheduleTasks(String ids) {
        return taskService.removeScheduleTasks(ids);
    }

    /**
     * 任务调度状态修改
     */
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @RequiresPermissions("monitor:job:changeStatus")
    @PostMapping("/changeStatus")
    public WebResponse doChangeTaskStatus(ScheduleTask task) {
        return taskService.doChangeTaskStatus(task);
    }

    /**
     * 任务调度立即执行一次
     */
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @RequiresPermissions("monitor:job:changeStatus")
    @PostMapping("/run")
    public WebResponse doScheduleTaskRunning(ScheduleTask task) {
        return taskService.doScheduleTaskRunning(task);
    }

    /**
     * 新增保存调度
     */
    @Log(title = "定时任务", businessType = BusinessType.INSERT)
    @RequiresPermissions("monitor:job:add")
    @PostMapping("/add")
    public WebResponse addScheduleTaskSave(@Validated ScheduleTask task) {
        return taskService.addScheduleTaskSave(task);
    }

    /**
     * 修改保存调度
     */
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @RequiresPermissions("monitor:job:edit")
    @PostMapping("/edit")
    public WebResponse modifyScheduleTask(@Validated ScheduleTask task)  {
        return taskService.modifyScheduleTask(task);
    }

    /**
     * 校验cron表达式是否有效
     */
    @PostMapping("/checkCronExpressionIsValid")
    public boolean checkCronExpressionValid(ScheduleTask task) {
        return taskService.checkCronExpressionValid(task);
    }

}